package com.lucher.tangstore.service.impl;

import com.lucher.tangstore.common.Tools;
import com.lucher.tangstore.dao.ProductdepotMapper;
import com.lucher.tangstore.dao.ProductinoutMapper;
import com.lucher.tangstore.po.Coderule;
import com.lucher.tangstore.po.Productdepot;
import com.lucher.tangstore.po.Productinout;
import com.lucher.tangstore.service.ICoderuleService;
import com.lucher.tangstore.service.IProductdepotService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


/**
 * @Title ProductdepotServiceImpl.java
 * @version 1.0 产品库存实现类
 */
@Service("productdepotService")
public class ProductdepotServiceImpl implements IProductdepotService {
	@Autowired
	private ProductdepotMapper productdepotDao;
	@Autowired
	private ProductinoutMapper productinoutDao;
	@Autowired
	private ICoderuleService coderuleService;
	//定义锁对象
	private Lock lock = new ReentrantLock();
	
	/**
	 * 产品入库
	 */
	public void saveProductdepot_instor(Productdepot productdepot) {
		
		try{
			
			//加锁
			lock.lock();
			
			//材料仓库ID
			Integer depotid = 0;
			
			//查询产品库存存不存在此种产品
			Productdepot depot = productdepotDao.findByBatchno(productdepot.getBatchno());
			
			//表示库存里有此种材料数据，修改库存量就行
			if(depot != null){
				Double depotamount = depot.getDepotamount() + productdepot.getDepotamount();
				depot.setDepotamount(depotamount);
				productdepotDao.updateByPrimaryKeySelective(depot);
				depotid = depot.getId();
			}else{//表示此次入库为新材料，新增加库存
				productdepotDao.insertSelective(productdepot);
				depotid = productdepot.getId();
			}
			
			//增加材料入库记录
			Productinout productinout = new Productinout();
			productinout.setProductcode(productdepot.getProductcode());
			productinout.setProductname(productdepot.getProductname());
			productinout.setOperatorcode(productdepot.getOperatorcode());
			productinout.setBatchno(productdepot.getBatchno());
			productinout.setType("入库");
			productinout.setInoutamount(productdepot.getDepotamount());
			productinout.setAddtime(Tools.getCurrentTime());
			
			productinoutDao.insertSelective(productinout);
			
		} finally {
			
			//释放锁
			lock.unlock();
		}
	}
	
	/**
	 * 产品出库
	 * return  0：无此产品库存
	 *        -1：库存量不够
	 *         1：出库成功
	 */
	public String saveProductdepot_outstor(Productdepot productdepot) {
		
		try{
			
			//加锁
			lock.lock();
			
			//材料仓库ID
			Integer depotid = 0;
			
			//查询产品库存存不存在此种产品
			Productdepot depot = productdepotDao.findByBatchno(productdepot.getBatchno());
			
			//表示库存里有此种材料数据，修改库存量就行
			if(depot != null){
				//将库存与出库量进行比较
				int flag = depot.getDepotamount().compareTo(productdepot.getDepotamount());
				
				if(flag >= 0){//库存够这次出库量
					Double depotamount = depot.getDepotamount() - productdepot.getDepotamount();
					depot.setDepotamount(depotamount);
					//修改库存量
					productdepotDao.updateByPrimaryKeySelective(depot);
					depotid = depot.getId();
					
					//增加材料出库记录
					Productinout productinout = new Productinout();
					productinout.setProductcode(depot.getProductcode());
					productinout.setProductname(depot.getProductname());
					productinout.setOperatorcode(productdepot.getOperatorcode());
					productinout.setBatchno(depot.getBatchno());
					productinout.setType("1");
					productinout.setInoutamount(productdepot.getDepotamount());
					productinout.setAddtime(Tools.getCurrentTime());
					
					productinoutDao.insertSelective(productinout);
					
					return "1";
					
				}else{
					return "-1";
				}
				
			}else{//表示此次入库为新材料，新增加库存
				return "0";
			}
			
		} finally {
			
			//释放锁
			lock.unlock();
		}
	}
	
	/**
	 * 获取产品唯一识别号
	 * 
	 * @param productcode
	 * @return
	 */
	public String getProductbatchno(String productcode){
		//材料批次号
		String batchno = coderuleService.getSystemcodenoByCodetypeStr(Coderule.CODETYPE_productidentfy);
		String startBatchno = StringUtils.substring(batchno, 0, batchno.length()-4);
		String endBatchno = StringUtils.substring(batchno, batchno.length()-4);
		//组装材料批次号
		return (startBatchno + productcode + endBatchno);
		
	}
	
	/**
	 * 查询数据库里有库存的各种材料批次号
	 * 
	 * @param form
	 * @return
	 */
	public HashMap<String,ArrayList<Productdepot>> findProductdepotHashmap(Productdepot form){
		HashMap<String,ArrayList<Productdepot>> productdepotHp = new HashMap<String,ArrayList<Productdepot>>();
		
		List<Productdepot> matedepotlist = productdepotDao.findByList(form);
		for (Productdepot productdepot : matedepotlist) {
			//从Hashmap中查找该编码
			ArrayList<Productdepot> productdepotList = productdepotHp.get(productdepot.getProductcode());
			if(productdepotList == null){
				productdepotList = new ArrayList<Productdepot>();
			}
			productdepotList.add(productdepot);
			productdepotHp.put(productdepot.getProductcode(), productdepotList);
		}
		return productdepotHp;
	}
	
	/**
	 * 从数据库里各种材料批次号HashMap中，获取符合条件的最早的那个批次号
	 * 
	 * @param form
	 * @return
	 */
	public Productdepot findProductdepotOldest(HashMap<String,ArrayList<Productdepot>> productdepotHp, Productdepot form){
		//无任何库存
		if(productdepotHp == null){
			return null;
		}
		//库存中没有该材料编码
		ArrayList<Productdepot> productdepotlist =  productdepotHp.get(form.getProductcode());
		if(productdepotlist == null || productdepotlist.size() == 0){
			return null;
		}
		
		Productdepot productdepotOldest = new Productdepot();
		
		for (Productdepot productdepot : productdepotlist) {
			if(StringUtils.isNotEmpty(productdepotOldest.getBatchno())){
				if(productdepotOldest.getBatchno().compareTo(productdepot.getBatchno()) > 0){//找到库存中最老的库存
					if(productdepot.getDepotamount() >= form.getInoutamount()){//库存中足够量出库
						Tools.setVOToVO(productdepot, productdepotOldest);
					}
				}
			}else{
				if(productdepot.getDepotamount() >= form.getInoutamount()){//库存中足够量出库
					Tools.setVOToVO(productdepot, productdepotOldest);
				}
			}
		}
		
		return productdepotOldest;
		
	}
}
